#ifndef METADATAUTILS_H
#define METADATAUTILS_H

#include <QMetaObject>
#include <QMetaClassInfo>
#include <QString>
#include <QMap>
#include "defaultbase.h"

class MetaDataUtils {
    Q_GADGET
public:
    //获取表名，这个是没问题
    template<typename T,typename = std::enable_if_t<std::is_base_of_v<DefaultBase, T>>>
    static QString tableName() {
        const QMetaObject* meta = &T::staticMetaObject;
        int classInfoIndex = meta->indexOfClassInfo("TableName");
        if (classInfoIndex != -1) {
            return meta->classInfo(classInfoIndex).value();
        }
        throw std::runtime_error("Table name not defined for entity");
    }

    //获取列名映射
    template<typename T>
    static QStringList columnMap() {
        QStringList columns;
        const QMetaObject* meta = &T::staticMetaObject;
        int columnIndex =  meta->indexOfClassInfo("Columns");
        if( -1 != columnIndex)
        {
            QString columnsName = meta->classInfo(columnIndex).value();
            columns = columnsName.split(",");
        }
        return columns;
    }

    //获取主键列名
    template<typename T>
    static QString getPrimaryKeyMap() {
        QStringList map = columnMap<T>();
        QString primaryKey ;
        const QMetaObject* meta = &T::staticMetaObject;
        int classInfoIndex = meta->indexOfClassInfo("PrimaryKey");
        if(-1 != classInfoIndex)
        {
            QString primaryKeyColumn = meta->classInfo(classInfoIndex).value() ;
            if(map.contains(primaryKeyColumn))
            {
                primaryKey =  primaryKeyColumn ;
            }
        }
        return primaryKey;
    }

    //获取自动增长列列名
    template<typename T>
    static QString getAutoIncrement() {
        QStringList map = columnMap<T>();
        QString autoIncrement ;
        const QMetaObject* meta = &T::staticMetaObject;
        int classInfoIndex = meta->indexOfClassInfo("AutoIncrement");
        if(-1 != classInfoIndex)
        {
            QString autoIncrementColumn = meta->classInfo(classInfoIndex).value() ;
            if(map.contains(autoIncrementColumn))
            {
                autoIncrement =  autoIncrementColumn ;
            }
        }
        return autoIncrement;
    }

    template<typename T,typename = std::enable_if_t<std::is_base_of_v<DefaultBase, T>>>
    static QVariantMap GetDefaultMapValue(const T& entity) {
        return entity.getDefault();
    }

    //获取列 默认比如，自增，插入的时候，系统时间
    template<typename T>
    static QStringList DefaultMap() {
        QStringList defaultColumns;
        const QMetaObject* meta = &T::staticMetaObject;
        int columnIndex = meta->indexOfClassInfo("DefaultColumns");
        if(-1 != columnIndex)
        {
            QString defaultColumnsName = meta->classInfo(columnIndex).value();
            defaultColumns = defaultColumnsName.split(",")   ;
        }
        return defaultColumns;
    }

    // 检查某一列是否是自增ID
    // template<typename T>
    // static bool isAutoIncrement(const QString& propertyName) {
    //     const QMetaObject* meta = &T::staticMetaObject;
    //     int index = meta->indexOfClassInfo((propertyName + "_AutoIncrement").toLatin1());
    //     return index != -1;
    // }

    // 检查某一列在插入数据的时候，是否可以缺省，默认是 系统时间
    // template<typename T>
    // static bool isDefault(const QString& propertyName) {
    //     const QMetaObject* meta = &T::staticMetaObject;
    //     int index = meta->indexOfClassInfo((propertyName + "_Default").toLatin1());
    //     return index != -1;
    // }

    // // 检查某一列在修改记录的的、的时候，是否可以缺省，默认是 系统时间
    // template<typename T>
    // static bool isUpdateTime(const QString& propertyName) {
    //     const QMetaObject* meta = &T::staticMetaObject;
    //     int index = meta->indexOfClassInfo((propertyName + "_UpdatedDefault").toLatin1());
    //     return index != -1;
    // }



};


#endif // METADATAUTILS_H
